package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-27
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R ListAll(){
        //int a = 1/0;
        return R.ok().data("teacherList",teacherService.list());
    }

    @ApiOperation("根据讲师删除")
    @DeleteMapping("remove/{id}")
    public R remove(@ApiParam("讲师Id") @PathVariable String id){
        if (teacherService.removeById(id)){
            return R.ok().success(true);
        }else {
            return R.error().success(false).message("根据id删除失败");
        }
    }

    @ApiOperation("分页查询")
    @PostMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录条数",required = true) @PathVariable Long limit,
                      @ApiParam(value = "讲师列表查询对象") @RequestBody TeacherQuery teacherQuery){
        //为什么都不用写service方法，因为mybatis-plus跟你封装了方法让你不用写
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel",pageModel);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    //required  是否为必传参数
    public R save(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b){
            return R.ok().message("根据id更新讲师成功");
        }else {
            return R.error().message("根据Id更新讲师失败");
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "讲师对象",required = true) @PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return R.ok().data("item",teacher);
        }else {
            return R.error().message("根据id获取失败");
        }
    }

    //批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("deleteAll")
    public R deleteAll(@ApiParam(value = "删除的id集合",required = true) @RequestBody List<String> ids){
        boolean b = teacherService.removeByIds(ids);
        if (b){
            return R.ok().message("批量删除成功");
        }else {
            return R.error().message("批量删除失败");
        }
    }

}

